package com.niuwa.p2p.action.producttemplate;

import java.util.List;

import org.apache.struts2.convention.annotation.Action;
import org.springframework.beans.factory.annotation.Autowired;

import com.niuwa.p2p.action.JsonAction;
import com.niuwa.p2p.core.mvc.entity.JsonResult;
import com.niuwa.p2p.entity.productTemplate.AreaPropCfg;
import com.niuwa.p2p.entity.productTemplate.GuaranteeUnit;
import com.niuwa.p2p.entity.productTemplate.ProductProtocol;
import com.niuwa.p2p.entity.productTemplate.ProductTemplate;
import com.niuwa.p2p.opr.utils.SessionUtil;
import com.niuwa.p2p.service.guarantee.IGuaranteeUnitService;
import com.niuwa.p2p.service.productTemplate.IInvestProdTemplateService;
import com.niuwa.p2p.service.productTemplate.ILoanProdTemplateService;
import com.niuwa.p2p.service.protocol.IProtocolService;
import com.niuwa.p2p.vo.productTemplate.LoanProdTmptForm;
import com.niuwa.p2p.vo.productTemplate.LoanProdTmptVo;
import com.niuwa.p2p.vo.protocol.ProductProtocolVo;
import com.opensymphony.xwork2.ActionContext;

public class LoanProductTmpltOperAction extends JsonAction {

	private static final long serialVersionUID = -164220153058789615L;

	@Autowired
	private IProtocolService protocolService;

	@Autowired
	private ILoanProdTemplateService ilptService;
	
	@Autowired
	private IInvestProdTemplateService investProdTemplateService;

	private LoanProdTmptForm ppv;

	/** 产品模板ID */
	private long tempId;
	
	/** 拒绝理由 */
	private String rejectReason;

	private List<AreaPropCfg> apcList;

	private List<ProductProtocol> ppList;

	@Autowired
	private IGuaranteeUnitService guaranteeService;
	
	/**
	 * 跳转到添加投资产品模板视图
	 * 
	 * @return 投资产品模板视图
	 * 
	 */
	@Action("gotoAddLoanProdTemplate")
	public String gotoAddLoanProdTemplate() {
		
		ActionContext context = ActionContext.getContext();
		
		List<GuaranteeUnit> guList = guaranteeService.searchAll();
		context.put("guList", guList);
		
		// 获取所有协议信息
		List<ProductProtocolVo> protocols = protocolService.selectAllProtocol();
		context.put("protocols", protocols);

		return "addLoanProdTemplate";
	}

	/**
	 * 添加借款产品模板
	 * 
	 * @return NONE
	 */
	@Action("addLoanProdTemplate")
	public String addLoanProdTemplate() {

		JsonResult result = new JsonResult();

		// 获取用户ID
		long userId = SessionUtil.getSessionUser().getId();
		
		String flag = ("on".equals(ppv.getIsCheckedOverdueFees())) ? "Y" : "N";
		ppv.setIsCheckedOverdueFees(flag);

		try {
			ProductTemplate productTemplate = new ProductTemplate();
			productTemplate.setTempName(ppv.getTempName());
			productTemplate.setMainVersion(Integer.parseInt(ppv.getMainVersion()));
			productTemplate.setMinorVersion(Integer.parseInt(ppv.getMinorVersion()));
			
			/*判断是否存在模版名称、版本号比输入的版本号高的模版*/
			int findProductTmptByName = investProdTemplateService.findProductTmptByName(productTemplate);
			if (findProductTmptByName > 0) {
				result.setSuccess(false);
				result.setMsg("创建的模板已存在且新版本号低于旧版本");
			}else {
				ilptService.addLoanProductTmplt(userId, ppv);
				result.setSuccess(true);
				result.setMsg("操作成功");
			}
		} catch (Exception e) {
			result.setSuccess(false);
			result.setMsg("操作失败");
		}
		this.setResult(result);
		return NONE;
	}

	/**
	 * 查看借款产品模板
	 * 
	 * @return viewLoanProdTemplate.jsp
	 */
	@Action("viewLoanProdTemplate")
	public String viewLoanProdTemplate() {

		ActionContext context = ActionContext.getContext();
		
		List<GuaranteeUnit> guList = guaranteeService.searchAll();
		context.put("guList", guList);
		
		LoanProdTmptVo prod = ilptService.getProdTmptById(tempId);

		ppv = prod.getTmptForm();
		ppv.setMinorVersion(String.valueOf(Long.valueOf(ppv.getMinorVersion()) - 1));
		context.put("ppv", ppv);

		apcList = prod.getApcList();
		ActionContext.getContext().put("apcList", apcList);

		ppList = prod.getPpList();
		ActionContext.getContext().put("ppList", ppList);

		return "viewLoanProdTemplate";
	}

	/**
	 * 修改投资产品模板
	 * 
	 * @return addLoanProdTemplate.jsp
	 */
	@Action("updateLoanProdTemplate")
	public String updateLoanProdTemplate() {
		
		ActionContext context = ActionContext.getContext();

		List<GuaranteeUnit> guList = guaranteeService.searchAll();
		context.put("guList", guList);
		
		LoanProdTmptVo prod = ilptService.getProdTmptById(tempId);

		ppv = prod.getTmptForm();
		context.put("ppv", ppv);

		apcList = prod.getApcList();
		context.put("apcList", apcList);

		ppList = prod.getPpList();
		context.put("ppList", ppList);

		List<ProductProtocolVo> protocols = protocolService.selectAllProtocol();
		context.put("protocols", protocols);

		return "addLoanProdTemplate";
	}
	
	/**
	 * 删除产品模板 
	 * 
	 * @return
	 * success: 操作成功
	 * fail:    后台Message(失败,产品模板不存在)
	 * @throws Exception 
	 */
	@Action("deleteLoanProductTmplt")
	public String deleteProductTmplt() throws Exception {
		JsonResult result = new JsonResult();		
		int num = investProdTemplateService.deleteProductTmplt(tempId);	
		if(num > 0){
			result.setSuccess(true);
			result.setMsg("操作成功");
		}else{
			result.setSuccess(false);
			result.setMsg("该模板不能删除");
		}
		this.setResult(result);
		return NONE;
	}

 	/**
	 * 产品模板审批通过 
	 * 
	 * @return
	 * success: 操作成功
	 * fail:    后台Message(失败,产品模板不存在)
	 * @throws Exception 
	 */
	@Action("passLaonAudit")
	public String passAudit() throws Exception {
		JsonResult result = new JsonResult();		
		investProdTemplateService.updateProductTmpltToPass(tempId, SessionUtil.getSessionUser().getId());
		result.setSuccess(true);
		result.setMsg("操作成功");
		this.setResult(result);
		return NONE;
	}

	
 	/**
	 * 产品模板审批拒绝 
	 * 
	 * @return
	 * success: 操作成功
	 * fail:    后台Message(失败,产品模板不存在)
	 * @throws Exception 
	 */
	@Action("rejectLoanAudit")
	public String rejectAudit() throws Exception {
		JsonResult result = new JsonResult();		
		investProdTemplateService.updateProductTmpltToReject(tempId, rejectReason, SessionUtil.getSessionUser().getId());		
		result.setSuccess(true);
		result.setMsg("操作成功");
		this.setResult(result);
		return NONE;
	}

	public LoanProdTmptForm getPpv() {
		return ppv;
	}

	public void setPpv(LoanProdTmptForm ppv) {
		this.ppv = ppv;
	}

	public long getTempId() {
		return tempId;
	}

	public void setTempId(long tempId) {
		this.tempId = tempId;
	}

	public List<AreaPropCfg> getApcList() {
		return apcList;
	}

	public void setApcList(List<AreaPropCfg> apcList) {
		this.apcList = apcList;
	}

	public List<ProductProtocol> getPpList() {
		return ppList;
	}

	public void setPpList(List<ProductProtocol> ppList) {
		this.ppList = ppList;
	}

	public String getRejectReason() {
		return rejectReason;
	}

	public void setRejectReason(String rejectReason) {
		this.rejectReason = rejectReason;
	}

}
